[#include "/WEB-INF/pages/common/taglibs.ftl"]
	
[#macro _script]
	<link rel="stylesheet" href="[@c.url value='/js/plugins/jquery-ztree/css/zTreeStyle/zTreeStyle.css'/]" type="text/css">
	<script type="text/javascript" src="[@c.url value='/js/plugins/jquery.alerts.js'/]"></script>
	<script type="text/javascript" src="[@c.url value='/js/plugins/jquery-ztree/js/jquery.ztree.core-3.1.js'/]"></script>
	<script type="text/javascript">
	
		var base = (function(){
			// 事件绑定
			function _addEventListeners() {
				$('a[act=edit]').click(function(){
					var id = $(this).parent().attr('tval');
					[@c.url var="editUrl" value='/manage/Category_edit.action' scope="page"]
						[@c.param name="channelId" value="${channelId}"/]
					[/@c.url]
					window.location = "${editUrl}&id=" + id;
				})
			
				$('a[act=delete]').click(function(){
					var id = $(this).parent().attr('tval');
					var title = $('#delete-confirm').attr('title');
					var content = $('#delete-confirm').html();
					jConfirm(content, title, function(r) {
						if (r) {
							[@c.url	var="deleteUrl" value='/manage/Category_delete.action' scope="page"]
								[@c.param name="backUrl" value="${backUrl}"/]
								[@c.param name="id" value=""/]
							[/@c.url]
							window.location	= "${deleteUrl}" + id;
						}
					});
					return false;
				})
            	
            	// 必须先渲染控件,后绑定事件
				//频道切换的select切换事件
				$('#channelSelect').change(function() {
					var id = $(this).val();
					$('form[name=channelForm]').attr('action', "[@c.url value='/manage/Category_list.action'][/@c.url]");
					$('form[name=channelForm]').submit();
				})
			}
			// 页面渲染
			function _treeRender () {
				
				var zNodes =[@s.action name="Category_tree" executeResult="true" namespace="/manage"]
			    		[@s.param name="ajax" value="true" /]
						[@s.param name="channelId" value="channelId" /]
			    	[/@s.action];
	            var setting = {
						data: {
							simpleData: {
								enable: true
							}
						},
						view: {
							// 渲染树
							addDiyDom: function (treeId, treeNode) {
								// 根节点以外的父节点
								//if (treeNode.pId != null) {
								//	$('#' + treeNode.tId).append("<a href='javascript:void(0);' onClick=\"return base.rmDir('" + treeNode.id + "')\">[删除]</a>");
								//}
								// 找到那个链接a
								var aObj = $("#" + treeNode.tId + "_a");
								if ($("#"+treeNode.id).length>0 || treeNode.pId == null) return;
								var editStr = "<span>&nbsp;&nbsp;&nbsp;&nbsp;</span><a href='javascript:void(0);' id='" + treeNode.tId +"_del' onClick=\"return base.rmDir('" + treeNode.id + "')\">[删除]</a>";
								aObj.append(editStr);
							}
						},
						callback: {
							onClick: function(event, treeId, treeNode, clickFlag){
								var isParent = treeNode.isParent;
								var id = treeNode.id;
								$.get(base.config.url, {'id':id, 'ajax':true, 'piece':true}, function(data){
				                	$('#categoryTableList').html(data);
				                	_addEventListeners();
				                });
							}
						}
					};
				$.fn.zTree.init($("#categoryTree"), setting, zNodes);
			}
			return {
                config: {
                    theme: null
                },
                rmDir : function(id) {
	            	// 删除确认
					jConfirm("删除栏目后,子栏目及所有文章都会被删除,是否继续此操作?", "警告", function(r) {
						if (r) {
							$('<form id="rmCateForm" method="post" action="[@c.url value='/manage/Category_delete.action'/]"></form>').prependTo(document.body);
							$('#rmCateForm').append("<input type='hidden' name='id' value='" + id + "' /><input type='hidden' name='cascade' value='true'/>");
							$('#rmCateForm').submit();
						}
					});
				},
                init: function () {
                	// 初始化colorbox
					common_box.init();
					// 初始化checkbox
					common_checkbox.init();
                    _addEventListeners();
                    
                },
                render : function() {
                	// 加载树数据
                	_treeRender();
                }
			}
		}());
	
		$(function(){
			base.config.url = "[@c.url value='/manage/Category_list.action?channelId=${channelId}'][/@c.url]";
			base.config.renderTo = "#categoryTableList";
			base.config.theme = getTheme();
            //Initializing the form
        	base.init();	
        	base.render();	
		})
	</script>
[/#macro]

[#if piece==true]
	<!--设置提交行为-->
	[#assign renderTo="#categoryTableList" /]
	[#include "/WEB-INF/pages/manage/Category/_categoryList.ftl" /]
[#else]
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Insert title here</title>
		<meta name="menu" content="CategoryMenu"/> 
		<meta name="heading" content="heading"/>
		[@_script /]
	</head>
	<body id="body">
		<div class="contenttitle radiusbottom0" style="margin-bottom:1px;">
	    	<h2 id="vertical" class="form"><span>栏目维护</span></h2>
	    </div><!--contenttitle-->
    	<div class="wizard verwizard2" style="">
        	<div class="verticalmenu widgetbox anchor" style="width:200px;"> 
        		<!--tree--> 
        		<div class="widgetcontent announcement">
        			<ul id="categoryTree" class="ztree" style=""></ul>
        		 </div>
		    </div>
		    [#assign renderTo="#categoryTableList"]
		    <div class="stepContainer" id="categoryTableList">
		    	[#include "/WEB-INF/pages/manage/Category/_categoryList.ftl"]
			</div>
			<div style="clear:both;"></div>
        </div>
	</body>
	</html>
[/#if]